package com.prim.update.notification;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.DrawableRes;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

/**
 * ================================================
 * 作    者：linksus
 * 版    本：1.0
 * 创建日期：6/20 0020
 * 描    述：通知管理类，用于显示下载通知，下载的进度通知，完成以及错误等通知
 * 修订历史：
 * ================================================
 */
public class NotificationHelper {
    private static final String TAG = "NotificationHelper";
    private Context context;
    public static final int NOTICE_ID_TYPE_0 = 1001;
    private NotificationCompat.Builder builder;
    private NotificationManager notificationManager;
    private Notification notification;

    private int mNotifyId = -1;

    private int largeIcon;

    private int smallIcon;

    private String contentTitle;

    private String contentText;

    @SuppressLint("StaticFieldLeak")
    private static NotificationHelper mNotificationHelper = null;


    private NotificationHelper() {
    }

    public static NotificationHelper getInstance() {
        if (mNotificationHelper == null) {
            synchronized (NotificationHelper.class) {
                if (mNotificationHelper == null) {
                    mNotificationHelper = new NotificationHelper();
                }
            }
        }
        return mNotificationHelper;
    }

    public NotificationHelper setNotifyId(int mNotifyId) {
        this.mNotifyId = mNotifyId;
        return this;
    }

    public NotificationHelper with(Context context) {
        this.context = context;
        return this;
    }

    public NotificationHelper setLargeIcon(@DrawableRes int largeIcon) {
        this.largeIcon = largeIcon;
        return this;
    }

    public NotificationHelper setSmallIcon(@DrawableRes int smallIcon) {
        this.smallIcon = smallIcon;
        return this;
    }

    public NotificationHelper setContentTitle(String contentTitle) {
        this.contentTitle = contentTitle;
        return this;
    }

    public NotificationHelper setContentText(String contentText) {
        this.contentText = contentText;
        return this;
    }


    public void clearNotification() {
        if (null != notificationManager) {
            notificationManager.cancel(mNotifyId);
        }
    }

    /**
     * 初始化通知栏
     */
    public NotificationHelper setNotifycation() {
        if (context == null) {
            Log.e(TAG, "setNotifycation: context is null ,please with(Context context)");
            return this;
        }
        if (mNotifyId == -1) {
            mNotifyId = NOTICE_ID_TYPE_0;
        }
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis();
        Bitmap bigBitmap = BitmapFactory.decodeResource(context.getResources(), largeIcon);
        builder = new NotificationCompat.Builder(context)
                .setSmallIcon(smallIcon)
                .setLargeIcon(bigBitmap)
                .setContentText(contentText)
                .setWhen(when)
                .setProgress(100, 0, false)
                .setContentTitle(contentTitle)
                .setAutoCancel(false)
                .setLights(Notification.FLAG_SHOW_LIGHTS, 1000, 1000);
        notification = builder.build();
        notificationManager.notify(mNotifyId, notification);
        return this;
    }

    public boolean isNotificationNull() {
        return notificationManager == null;
    }

    /**
     * 更新通知栏
     *
     * @param pro
     * @param msg
     */
    public void updateNotification(int pro, String msg) {
        if (builder != null && notification != null && notificationManager != null) {
            if (pro != -1) builder.setProgress(100, pro, false);
            builder.setContentText(msg);
            notification = builder.build();
            Intent clickIntent = new Intent(context, NotificationClickReceiver.class); //点击通知之后要发送的广播
            int id = (int) (System.currentTimeMillis() / 1000);
            PendingIntent contentIntent = PendingIntent.getBroadcast(context, id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            notification.contentIntent = contentIntent;
            notificationManager.notify(mNotifyId, notification);
        }
    }
}
